const app = getApp()
import BaseModel from '../../utils/common.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    roleCode:"",//角色  1 分总  2员工  3财务
    searchShow:false,//是否显示搜索弹窗
    searchValue:"", //搜索内容
    listarr: [],//搜索记录数组
    dataList:[],//获取到的数据
    posterData:"",//海报数据
    page:1,
    size:16, //每页条数
    code:"1",//导航标识  默认我负责的
    chose:'',//筛选 选择项
    fromuid:'',//负责人
    footer_more:"",
    empty_title:"",
    empty_img:"",
    empty_again:"",//重试
    is_clickTab:0, //重试或下拉是0，上啦是1
    navScore_id:0,//分数导航id
    start_points:0,//开始分数
    end_points:0,//结束分数
    is_important:0,//是否是重点客户筛选  0不是 1是
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.type_id==2){
      this.setData({
        code:options.type_id
      })
    }
    this.setData({
      chose: options.chose, //筛选
      fromuid: options.fromuid,
      navScore_id:options.navScore_id,

    })
    console.log(options.start_points)
    console.log(options.end_points)
    if(options.start_points!=undefined||options.end_points!=undefined){
      this.setData({
        start_points:options.start_points,
        end_points:options.end_points,
      })
    }
    // this.getCustomerMesData(); //获取数据
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      dataList: [],
      page:1,
      is_clickTab:0,
      roleCode: wx.getStorageSync('roleCode'),
      listarr:wx.getStorageSync('list_arr') || [],
      is_important:0
    })
    this.getLevelData()//获取角色
    if(this.data.roleCode==3||this.data.roleCode==4){ //财务 获取数据
      this.getFinanceMesData()
    }else{
      if(this.data.start_points==0&&this.data.end_points==0){
        this.getCustomerMesData(); //员工和分总 获取数据
      }else{
        this.setData({
          is_important:1
        })
        this.getImportantCustomerData();//获取重要用户数据
      }
    }
    if(this.data.fromuid||this.data.chose){
      this.setData({
        searchValue: ""
      })
    }
  },
  //获取角色
  getLevelDataModel:new BaseModel({
    url:"getLevel",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getLevelData(){
    this.getLevelDataModel
        .fetch({  })
        .then(res => {
          //成功 (1分总2普通员工)
          app.globalData.role=res.data;
          wx.setStorageSync('roleCode',res.data)
        })
        .catch(res => {
        })
  },
  /*导航切换*/
  clickNav:function(e){
    this.setData({
      dataList: [],
      is_clickTab:0,
      chose: "",
      fromuid: "",
      searchValue: "",
      page:1,
      code:e.currentTarget.dataset.id,
      navScore_id:0,//分数导航id
      start_points:0,//开始分数
      end_points:0,//结束分数
      is_important:0
    })
    this.getCustomerMesData(); //员工和分总 获取数据
  },
  //员工和分总 获取重要用户数据
  getImportantCustomerModel:new BaseModel({
    url: "employees/MemberData/listMemberData",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getImportantCustomerData(){
    this.getImportantCustomerModel
        .fetch({
          type_id:this.data.code,
          page:this.data.page,
          size:this.data.size,
          fromuid:"",
          chose:"",
          keyword:"",
          start_points:this.data.start_points,//开始分数
          end_points:this.data.end_points,//结束分数
        })
        .then(res => {
          wx.removeStorage({
            key: 'chose',
            success: function (res) {
            },
          })
          wx.removeStorage({
            key: 'fromuid',
            success: function (res) {
            },
          })
          if(this.data.is_clickTab==0){
            this.setData({
              dataList:res.data,
              empty_title:"暂无数据",
              empty_img:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon_new3.0/empty_mes.png",
              empty_again:"",//重试
            })
          }else{
            if(res.data==""||res.data==null){
              this.setData({
                footer_more:"没有更多数据了"
              })
            }else{
              //成功
              var dataList_list = this.data.dataList;
              for(var i=0;i<res.data.length;i++){
                dataList_list.push(res.data[i])
              }
              this.setData({
                dataList:dataList_list
              })
            }
          }
        })
        .catch(res => {
          this.setData({
            empty_title:"请求失败",
            empty_img:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon/empty_data.png",
            empty_again:"重试",//重试
          })
        })
  },
  //员工和分总 获取数据
  getCustomerMesModel:new BaseModel({
    url: "getMemberList",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getCustomerMesData(){
    this.getCustomerMesModel
        .fetch({
          type_id:this.data.code,
          page:this.data.page,
          size:this.data.size,
          fromuid:this.data.fromuid,
          chose:this.data.chose,
          keyword:this.data.searchValue
        })
        .then(res => {
          wx.removeStorage({
            key: 'chose',
            success: function (res) {
            },
          })
          wx.removeStorage({
            key: 'fromuid',
            success: function (res) {
            },
          })
          if(this.data.is_clickTab==0){
            this.setData({
              dataList:res.data,
              empty_title:"暂无数据",
              empty_img:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon_new3.0/empty_mes.png",
              empty_again:"",//重试
            })
          }else{
            if(res.data==""||res.data==null){
              this.setData({
                footer_more:"没有更多数据了"
              })
            }else{
              //成功
              var dataList_list = this.data.dataList;
              for(var i=0;i<res.data.length;i++){
                dataList_list.push(res.data[i])
              }
              this.setData({
                dataList:dataList_list
              })
            }
          }
        })
        .catch(res => {
          this.setData({
            empty_title:"请求失败",
            empty_img:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon/empty_data.png",
            empty_again:"重试",//重试
          })
        })
  },

  //财务 获取数据
  getFinanceMesModel:new BaseModel({
    url: "employees/allMember",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getFinanceMesData(){
    this.getFinanceMesModel
        .fetch({
          page:this.data.page,
          size:this.data.size,
          fromuid:this.data.fromuid,
          chose:this.data.chose,
          keyword:this.data.searchValue
        })
        .then(res => {
          wx.removeStorage({
            key: 'chose',
            success: function (res) {
            },
          })
          wx.removeStorage({
            key: 'fromuid',
            success: function (res) {
            },
          })
          if(this.data.is_clickTab==0){
            this.setData({
              dataList:res.data,
              empty_title:"暂无数据",
              empty_img:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon_new3.0/empty_mes.png",
              empty_again:"",//重试
            })
          }else{
            if(res.data==""||res.data==null){
              this.setData({
                footer_more:"没有更多数据了"
              })
            }else{
              //成功
              var dataList_list = this.data.dataList;
              for(var i=0;i<res.data.length;i++){
                dataList_list.push(res.data[i])
              }
              this.setData({
                dataList:dataList_list
              })
            }
          }
        })
        .catch(res => {
          this.setData({
            empty_title:"请求失败",
            empty_img:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon/empty_data.png",
            empty_again:"重试",//重试
          })
        })
  },

  /*获取海报数据*/
  getPosterModel:new BaseModel({
    url:"employees/dataCompletion",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getPosterData(){
    this.getPosterModel
        .fetch({})
        .then(res => {
          //成功
          this.setData({
            posterData:res.data,
          })
          if (app.globalData.posterData == "") {
            wx.showToast({
              title: '海报生成中，约30秒',
              icon: 'none',
              duration: 2000
            })
          } else {
            wx.showLoading({
              title: '海报生成中...',
            });
            this.setData({
              isshow: true,
              template: this.palette()
            })
          }
        })
        .catch(res => {
          //失败
        })
  },

  /*添加客户资料*/
  customer_add:function(){
    wx.navigateTo({
      url: './customer_add'
    })
  },

  /*分享海报*/
  sharePoster:function(){
    this.getPosterData();
  },
  onImgOK(e) {
    wx.hideLoading();
    this.imagePath = e.detail.path;
  },
  /*点击隐藏海报*/
  share_posterHide:function(){
    this.setData({
      isshow:false,
    })
  },
  /*长按保存图片*/
  longTap: function(e){
    var that =this;
    wx.showModal({
      title: '提示',
      content: '保存分享',
      success(res) {
        if (res.confirm) {
          wx.saveImageToPhotosAlbum({
            filePath: that.imagePath,
            success(res) {
              wx.showToast({
                title: '保存成功',
                icon: 'success',
                duration: 2000
              })
            },
            fail(res) {
              wx.showToast({
                title: '保存失败，请重试',
                icon: 'none',
                duration: 2000
              })
            },
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  /*保存图片*/
  saveImage() {
    wx.saveImageToPhotosAlbum({
      filePath: this.imagePath,
      success(res) {
        wx.showToast({
          title: '保存成功',
          icon: 'success',
          duration: 2000
        })
      },
      fail(res) {
        wx.showToast({
          title: '保存失败，请重试',
          icon: 'none',
          duration: 2000
        })
      },
    });
  },
  /*禁止页面滚动*/
  preventTouchMove:function(e) {

  },
  palette() {
    return ({
      width: '630rpx',
      height: '920rpx',
      background: '/images/share_posterBg1.png',
      views: [{
          type:"image",  //头像
          url: this.data.posterData.staff_head,
          css:[{
            top:"96rpx",
            left:"262rpx",
            width:"110rpx",
            height:"110rpx",
            borderRadius:"66rpx",
            borderWidth:0
          }]
        },
        {
          type:"text",
          text:this.data.posterData.staff_name,
          css:[{
            top:"260rpx",
            left:"190rpx",
            fontSize:"28rpx",
            fontWeight: 'bold',
            color:"#666"
          }]
        },
        {
          type:"text",
          text:"邀请您补全资料",
          css:[{
            top:"260rpx",
            left:"290rpx",
            fontSize:"26rpx",
            color:"#666"
          }]
        },
        {
          type:"text",
          text:this.data.posterData.activity_title,
          css:[{
            top:"320rpx",
            align: 'center',
            width: '400rpx',
            left: '310rpx',
            maxLines: 2,
            fontSize:"32rpx",
            lineHeight:"46rpx",
            fontWeight: 'bold'
          }]
        },
        {
          type:"image",  //活动图
          url: this.data.posterData.completion,
          css:[{
            top:"350rpx",
            left:"120rpx",
            width:"390rpx",
            height:"195rpx",
            borderRadius:"10rpx",
            borderWidth:0
          }]
        },
        {
          type:"image",  //二维码图片
          url: this.data.posterData.staff_code,
          css:[{
            bottom:"120rpx",
            left:"250rpx",
            width:"160rpx",
            height:"160rpx",
          }]
        },
        {
          type:"text",
          text: "老用户扫码领取复学会员",
          css:[{
            align: 'center',
            bottom:"80rpx",
            left:"320rpx",
            fontSize:"20rpx",
            color:"#666"
          }]
        }
      ],
    });
  },

  /*将搜索框的值赋值给value*/
  inputvalue:function(e){
    this.setData({
      searchValue:e.detail.value
    })
  },
  /*点击确定搜索*/
  searchText:function(){
    var that = this;
    let searchValue = that.data.searchValue;
    let arr = that.data.listarr;
    if(searchValue==""){
      wx.showToast({
        title: '搜索不能为空',
        icon: 'none'
      })
    }else{
      if(arr.length<10){
        arr.unshift({
          value:searchValue
        })
      }else{
        arr.pop();
        arr.unshift({
          value:searchValue
        })
      }
      this.setData({
        listarr:arr, //搜索数组
        searchValue:searchValue, //当前搜索值
        searchShow:false,
        is_important:0
      })
      if(this.data.roleCode==3||this.data.roleCode==4){ //财务 获取数据
        this.getFinanceMesData()
      }else{
        this.getCustomerMesData(); //员工和分总 获取数据
      }
      wx.setStorageSync('list_arr', arr) //存到缓存
    }
  },
  /*点击搜索,展示弹窗*/
  keywordSearch:function(){
    this.setData({
      searchShow:true,
      searchValue:"", //搜索内容
      chose:'',//筛选 选择项
    })
  },
  /*清空搜索，筛选*/
  empty_search:function(){
    var that = this;
    that.setData({
      searchValue:"", //搜索内容
      chose:'',//筛选 选择项
      searchShow:false
    })
    console.log(that.data.searchShow)
    if(that.data.roleCode==3||this.data.roleCode==4){ //财务 获取数据
      that.getFinanceMesData()
    }else{
      that.getCustomerMesData(); //员工和分总 获取数据
    }
  },
  /*搜索历史*/
  clickHistory:function(e){
    this.setData({
      searchValue:e.currentTarget.dataset.id,
      searchShow:false,
      is_important:0
    })
    if(this.data.roleCode==3||this.data.roleCode==4){ //财务 获取数据
      this.getFinanceMesData()
    }else{
      this.getCustomerMesData(); //员工和分总 获取数据
    }
  },
  //点击弹窗灰色部分隐藏
  clickMaskBg:function(){
    this.setData({
      searchShow:false
    })
  },
  //重试
  again_request:function(){
    this.setData({
      is_clickTab:0,
      page:1,
      is_important:0
    })
    this.getLevelData()
    if(this.data.roleCode==3||this.data.roleCode==4){ //财务 获取数据
      this.getFinanceMesData()
    }else{
      if(this.data.start_points==0&&this.data.end_points==0){
        this.getCustomerMesData(); //员工和分总 获取数据
      }else{
        this.setData({
          is_important:1
        })
        this.getImportantCustomerData();//获取重要用户数据
      }
    }
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      is_clickTab:0,
      page:1,
      is_important:0
    })
    this.getLevelData();
    if(this.data.roleCode==3||this.data.roleCode==4){ //财务 获取数据
      this.getFinanceMesData()
    }else{
      if(this.data.start_points==0&&this.data.end_points==0){
        this.getCustomerMesData(); //员工和分总 获取数据
      }else{
        this.setData({
          is_important:1
        })
        this.getImportantCustomerData();//获取重要用户数据
      }
    }
    wx.stopPullDownRefresh(); //停止下拉
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    that.data.page+=1;
    that.setData({
      is_clickTab:1,
      is_important:0
    })
    if(that.data.roleCode==3||that.data.roleCode==4){ //财务 获取数据
      that.getFinanceMesData()
    }else{
      if(that.data.start_points==0&&that.data.end_points==0){
        that.getCustomerMesData(); //员工和分总 获取数据
      }else{
        that.setData({
          is_important:1
        })
        that.getImportantCustomerData();//获取重要用户数据
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
      
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

})